# -----------------  Baseline SVM  -----------------
from sklearn import svm
import time
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
from dataset import get_satellite

def _supervised_svm(Lx, Ly, test_x, test_y,
                       kernel='linear', C=1.5, gamma='scale'):
    """
    只用 (Lx, Ly) 训练一个普通 SVM，返回：
        acc, (precision, recall, f1), train_time
    """
    clf = svm.SVC(C=C, kernel=kernel, gamma=gamma)

    tic = time.perf_counter()
    clf.fit(Lx, Ly)
    train_t = time.perf_counter() - tic

    y_pred = clf.predict(test_x)
    acc = accuracy_score(test_y, y_pred)
    prec, rec, f1, _ = precision_recall_fscore_support(
        test_y, y_pred, average='binary', pos_label=1)

    return acc, (prec, rec, f1), train_t

if __name__ == '__main__':
    from dataset import get_ionosphere

    Lx, Ly, Ux, Uy = get_ionosphere(n_labeled=50)
    sup_acc, (prec, rec, f1), sup_time = _supervised_svm(
        Lx, Ly, Ux, Uy, kernel='linear', C=1.5)
    print(f'Supervised SVM     acc={sup_acc:.4f}  '
          f'f1={f1:.4f}  train={sup_time:.3f}s')